package com.xls.box2023.login;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.lzz.bottomencapsulation.BuildConfig;
import com.lzz.bottomencapsulation.utils.TUtils;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.xls.box2023.mvp.BasePresenterImpl;
import com.xls.box2023.wxshare.WxShareUtils;

import java.util.List;

/**
 * MVPPlugin
 */

public class LoginPresenter extends BasePresenterImpl<LoginContract.View> implements LoginContract.Presenter{
    /**
     * //申请登陆时获取设备信息得权限
     */
    @Override
    public void getPermissions() {
        XXPermissions.with(activity)
                // 申请单个权限
                .permission(Permission.READ_PHONE_STATE)
                .permission(Permission.READ_MEDIA_IMAGES)
                .permission(Permission.READ_PHONE_NUMBERS)
                .permission(Permission.READ_MEDIA_AUDIO)
                .permission(Permission.READ_MEDIA_VIDEO)
                .request(new OnPermissionCallback() {

                    @Override
                    public void onGranted(List<String> permissions, boolean all) {
                        if (all) {
                            if (!WxShareUtils.getInstance().isWeixinAvilible(activity)){
                                TUtils.showShort(activity,"请安装微信");
                                return;
                            }
//                                            微信授权登陆
                            SendAuth.Req req = new SendAuth.Req();
                            req.scope = "snsapi_userinfo";
                            req.state = "box2023";
                            api.sendReq(req);
                        }
                    }

                    @Override
                    public void onDenied(List<String> permissions, boolean never) {
                        if (never) {
                            XXPermissions.startPermissionActivity(activity, permissions);
                        } else {
                            TUtils.showShort(context,"请授予权限否则无法登陆");
                        }
                    }
                });
    }

    // IWXAPI 是第三方app和微信通信的openApi接口
    private IWXAPI api;

    @Override
    public void regToWx() {
        // 通过WXAPIFactory工厂，获取IWXAPI的实例
        api = WXAPIFactory.createWXAPI(context, BuildConfig.WX_APP_ID, true);
        // 将应用的appId注册到微信
        api.registerApp(BuildConfig.WX_APP_ID);
        //建议动态监听微信启动广播进行注册到微信
        activity.registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                // 将该app注册到微信
                api.registerApp(BuildConfig.WX_APP_ID);
            }
        }, new IntentFilter(ConstantsAPI.ACTION_REFRESH_WXAPP));

    }
}
